package cn.com.gridinfo.sooc.core.course.note.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.note.bean.Note;
import cn.com.gridinfo.sooc.utils.widget.MyExpandTextView;

/**
 * 笔记 adapter
 * Created by yingchunli on 2015/12/31.
 */
public class NoteAdapter extends UltimateViewAdapter<NoteAdapter.ViewHolder> {

    private List<Note> noteList;
    private Context context;

    public NoteAdapter(List<Note> noteList, Context context) {
        this.noteList = noteList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.course_note_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= noteList.size() : position < noteList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            holder.bjnr.setText(noteList.get(position).getBjnr());

            if (noteList.get(position).getIsdz() == 1) {
                //已赞
                holder.zanIcon.setImageResource(R.mipmap.dianzan_icon_pass);
                holder.zannum.setTextColor(context.getResources().getColor(R.color.colorAccent));
            } else {
                holder.zanIcon.setImageResource(R.mipmap.dianzan_icon_normal);
                holder.zannum.setTextColor(context.getResources().getColor(R.color.color_666));
            }
            holder.author.setText(noteList.get(position).getName());
            holder.time.setText(noteList.get(position).getTime_show());
            holder.zannum.setText(noteList.get(position).getZanCount()+"");
            holder.note_dianzan_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 去点赞
                    mOnItemClickLitener.OnItemClick(v, position);
                }
            });

        }
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void loadmore(List<Note> list) {
        noteList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Note> list) {
        noteList.clear();
        noteList.addAll(list);
        notifyDataSetChanged();
    }

    public Note getObject(int position) {
        return noteList.get(position);
    }

    public void updateObject(int position, int isdz) {
        if (isdz == 1) {
            noteList.get(position).setZanCount(noteList.get(position).getZanCount() + 1);
        } else {
            noteList.get(position).setZanCount(noteList.get(position).getZanCount() - 1);
        }
        noteList.get(position).setIsdz(isdz);
        notifyDataSetChanged();
    }

    @Override
    public int getAdapterItemCount() {
        return noteList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        MyExpandTextView bjnr;//笔记内容
//        TextView toggle_bt;//全文 收起
        TextView author;//发布人
        TextView time;//时间
        TextView zannum;//赞的个数
        ImageView zanIcon;//赞的图标

        RelativeLayout note_dianzan_layout;//点赞区域

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                bjnr = (MyExpandTextView) itemView.findViewById(R.id.toggle_text_view);
                author = (TextView) itemView.findViewById(R.id.author_note_item);
                time = (TextView) itemView.findViewById(R.id.time_note_item);
                zannum = (TextView) itemView.findViewById(R.id.zannum_note_item);
                zanIcon = (ImageView) itemView.findViewById(R.id.zanIcon_note_item);
                note_dianzan_layout = (RelativeLayout)itemView.findViewById(R.id.note_dianzan_layout);
            }
        }


    }


}
